(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.resourceBundle.downloadInitialController', downloadInitialController);
	downloadInitialController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
	                                    'storageService', 'commonService', 'resourceBundleService' ];
	function downloadInitialController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, storageService, commonService, resourceBundleService) {
		var ctrl = this, resourceBundleInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.resourceBundle = resourceBundleInfo;
		
		ctrl.init = function() {
			authService.downloadInitial().then(function(response) {
				ctrl.refBundleOptions = response.data.bundleList;
				ctrl.resourceBundle.siteCode = storageService.getUserInfo().siteCode;
				if( !ctrl.resourceBundle.refBundleId){
					ctrl.resourceBundle.refBundleId = response.data.bundleList.data[0].value;
				}
			});
			resourceBundleService.getResourceBundleScopeOptions().then(function(response) {
				ctrl.scopeOptions = response.data;
				if( !ctrl.resourceBundle.scope){
					ctrl.resourceBundle.scope = response.data[0].value;
				}
			});
		};

		ctrl.refBundleChanged = function() {
			if(ctrl.refBundleOptions != null && ctrl.refBundleOptions.data != null){
				angular.forEach(ctrl.refBundleOptions.data, function(refBundle, key){
					if(ctrl.resourceBundle.refBundleId  == refBundle.value){
						ctrl.resourceBundle.refBundleDesc = refBundle.extProperty;
					}
				});
			}
		}; 
		
		ctrl.download = function() {
			ctrl.resourceBundle.isTemporary = "N";
			commonService.download(ctrl.resourceBundle, 'do/cda/resourceBundle/download');
		};

		function downloadSuccessful(response) {
//			delete ctrl.piLoad.fileList;//clear file list
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'messages.resourcebundle.download.success' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.resourceBundle.download');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.resourcebundledownload.list',
					'state' : 'cda.resourceBundle.list',
					'params' : {}
				}]
			});
		}

		function downloadFailed(response) {
			if(!response.error || response.error == ''){
				response.error = 'resourcebundle.file.content.invalid';
			}
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		ctrl.scopeChanged = function() {
			if(ctrl.resourceBundle.scope){
				resourceBundleService.getlocaleCodeOptions(ctrl.resourceBundle.scope).then(function(response) {
					if(response){
						ctrl.localOptions = response.data;
					}
					if( !ctrl.resourceBundle.localeCode){
						ctrl.resourceBundle.localeCode = response.data[0].value;
					}
				});
			}
		};
	}
})(angular);